const base = {};

base.trackParsingActionId = "";

base.sendAjaxRequest = (method, url, body = null, file = null) => {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open(method, url);

    xhr.responseType = "json";

    setHeaders(xhr, body, file);
    xhr.onload = () => resolve(xhr.response);
    sendResponse(xhr);
  });

  // НИЖЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

  function sendResponse(xhr) {
    if (body) {
      xhr.send(JSON.stringify(body));
    } else if (file) {
      const formData = new FormData();
      formData.append("file", file.fileData, file.fileName);
      xhr.send(formData);
    } else {
      xhr.send();
    }
  }

  function setHeaders(xhr, body, file) {
    // Устанавливаем заголовки

    if (body) {
      xhr.setRequestHeader("X-CSRFToken", body.token);
      xhr.setRequestHeader("Content-Type", "application/json");
    } else if (file) {
      xhr.setRequestHeader("X-CSRFToken", file.token);
    }
  }
};
